import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["format", "onEnd"];
import React, { Component } from 'react';

function fixedZero(val) {
  return val * 1 < 10 ? "0".concat(val) : val;
}

var CountDown = /*#__PURE__*/function (_Component) {
  _inherits(CountDown, _Component);

  var _super = _createSuper(CountDown);

  function CountDown(_props) {
    var _this;

    _classCallCheck(this, CountDown);

    _this = _super.call(this, _props);
    _this.timer = 0;
    _this.interval = 1000;

    _this.initTime = function (props) {
      var lastTime = 0;
      var targetTime = 0;

      try {
        if (Object.prototype.toString.call(props.target) === '[object Date]') {
          targetTime = props.target.getTime();
        } else {
          targetTime = new Date(props.target).getTime();
        }
      } catch (e) {
        throw new Error('invalid target prop', e);
      }

      lastTime = targetTime - new Date().getTime();
      return {
        lastTime: lastTime < 0 ? 0 : lastTime
      };
    };

    _this.defaultFormat = function (time) {
      var hours = 60 * 60 * 1000;
      var minutes = 60 * 1000;
      var h = Math.floor(time / hours);
      var m = Math.floor((time - h * hours) / minutes);
      var s = Math.floor((time - h * hours - m * minutes) / 1000);
      return /*#__PURE__*/React.createElement("span", null, fixedZero(h), ":", fixedZero(m), ":", fixedZero(s));
    };

    _this.tick = function () {
      var onEnd = _this.props.onEnd;
      var lastTime = _this.state.lastTime;
      _this.timer = setTimeout(function () {
        if (lastTime < _this.interval) {
          clearTimeout(_this.timer);

          _this.setState({
            lastTime: 0
          }, function () {
            if (onEnd) {
              onEnd();
            }
          });
        } else {
          lastTime -= _this.interval;

          _this.setState({
            lastTime: lastTime
          }, function () {
            _this.tick();
          });
        }
      }, _this.interval);
    };

    var _this$initTime = _this.initTime(_props),
        _lastTime = _this$initTime.lastTime;

    _this.state = {
      lastTime: _lastTime
    };
    return _this;
  }

  _createClass(CountDown, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.tick();
    } //  eslint-disable-next-line

  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      var _this2 = this;

      var target = this.props.target;

      if (target !== nextProps.target) {
        clearTimeout(this.timer);

        var _this$initTime2 = this.initTime(nextProps),
            lastTime = _this$initTime2.lastTime;

        this.setState({
          lastTime: lastTime
        }, function () {
          _this2.tick();
        });
      }
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      clearTimeout(this.timer);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          _this$props$format = _this$props.format,
          format = _this$props$format === void 0 ? this.defaultFormat : _this$props$format,
          onEnd = _this$props.onEnd,
          rest = _objectWithoutProperties(_this$props, _excluded);

      var lastTime = this.state.lastTime;
      var result = format(lastTime);
      return /*#__PURE__*/React.createElement("span", rest, result);
    }
  }]);

  return CountDown;
}(Component);

export default CountDown;